package com.softtown.modules.workflow.controller;

import com.softtown.common.annotation.LogOperation;
import com.softtown.common.exception.ErrorCode;
import com.softtown.common.utils.Result;
import com.softtown.modules.workflow.dto.*;
import com.softtown.modules.workflow.entity.SwfDocPhaseDefEntity;
import com.softtown.modules.workflow.service.SwfDocPhaseDefService;
import com.softtown.modules.workflow.service.SwfLastAprvReqService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author: Dingyayong
 * @Date: 2019/9/20 17:08
 * @Description:
 */
@RestController
@RequestMapping("swf/lastAprvReq")
@Api(tags = "终审要求")
public class SwfLastAprvReqController {

    @Resource
    private SwfLastAprvReqService swfLastAprvReqService;

    @Resource
    private SwfDocPhaseDefService swfDocPhaseDefService;

    /**
     * 添加最终审设置
     *
     * @param swfLastAprvReqAddDTO 添加终审者资格要求DTO
     * @return com.softtown.common.utils.Result
     * @Author Dingyayong
     * @Date 2020/4/13 17:05
     */
    @PostMapping("addSwfLastAprvReq")
    @ApiOperation("添加最终审设置")
    @LogOperation("添加最终审设置")
    public Result addSwfLastAprvReq(@RequestBody SwfLastAprvReqAddDTO swfLastAprvReqAddDTO) {
        Result result = new Result<>();
        int size = swfLastAprvReqAddDTO.getTableData().size();
        List<SwfDocPhaseDefEntity> swfDocPhaseDefEntities
                = swfDocPhaseDefService.checkDocPhaseDef(swfLastAprvReqAddDTO.getCompId(),swfLastAprvReqAddDTO.getDocTypeValue());
        if (swfDocPhaseDefEntities.size() > 0) {
            return result.error(ErrorCode.DOC_TYPE_ERROR);
        } else {
            List<Long> approveId = new ArrayList<>();
            for (SwfLastAprvReqAddDataDTO swfLastAprvReqAddDataDTO : swfLastAprvReqAddDTO.getTableData()) {
                approveId.add(swfLastAprvReqAddDataDTO.getApproval());
            }
            HashSet set = new HashSet(approveId);
            approveId.clear();
            approveId.addAll(set);
            int setSize = approveId.size();
            if (setSize < size) {
                return result.error(ErrorCode.PHASE_DOC_REPEAT);
            }
            swfLastAprvReqService.addSwfLastAprvReq(swfLastAprvReqAddDTO);
        }
        return result;
    }

    /**
     * 获取当前最终审设置
     *
     * @param swfLastAprvReqDTO 终审者资格要求DTO
     * @return com.softtown.common.utils.Result<com.softtown.modules.dto.SwfLastAprvReqAddDTO>
     * @Author Dingyayong
     * @Date 2020/4/13 17:07
     */
    @PostMapping("getSwfLastAprvReqInfo")
    @ApiOperation("获取当前最终审设置")
    @LogOperation("获取当前最终审设置")
    public Result<SwfLastAprvReqAddDTO> getSwfLastAprvReqInfo(@RequestBody SwfLastAprvReqDTO swfLastAprvReqDTO) {
        SwfLastAprvReqAddDTO swfLastAprvReqEntities =
                swfLastAprvReqService.getSwfLastAprvReqInfo(swfLastAprvReqDTO.getCompId(), swfLastAprvReqDTO.getDocTypeValue());
        return new Result<SwfLastAprvReqAddDTO>().ok(swfLastAprvReqEntities);
    }

    /**
     * 修改最终审设置
     *
     * @param swfLastAprvReqAddDTO 修改终审者资格要求DTO
     * @return com.softtown.common.utils.Result
     * @Author Dingyayong
     * @Date 2020/4/13 17:09
     */
    @PostMapping("editSwfLastAprvReq")
    @ApiOperation("修改最终审设置")
    @LogOperation("修改最终审设置")
    public Result editSwfLastAprvReq(@RequestBody SwfLastAprvReqAddDTO swfLastAprvReqAddDTO) {
        Result result = new Result<>();
        int size = swfLastAprvReqAddDTO.getTableData().size();
        List<Long> approveId = new ArrayList<>();
        for (SwfLastAprvReqAddDataDTO swfLastAprvReqAddDataDTO : swfLastAprvReqAddDTO.getTableData()) {
            approveId.add(swfLastAprvReqAddDataDTO.getApproval());
        }
        HashSet set = new HashSet(approveId);
        approveId.clear();
        approveId.addAll(set);
        int setSize = approveId.size();
        if (setSize < size) {
            return result.error(ErrorCode.PHASE_DOC_REPEAT);
        }
        swfLastAprvReqService.editSwfLastAprvReq(swfLastAprvReqAddDTO);
        return result;
    }

    /**
     * 删除最终审设置
     *
     * @param swfLastAprvReqDTO 终审者资格要求DTO
     * @return com.softtown.common.utils.Result
     * @Author Dingyayong
     * @Date 2020/4/13 17:10
     */
    @PostMapping("deleteSwfLastAprvReq")
    @ApiOperation("删除最终审设置")
    @LogOperation("删除最终审设置")
    public Result deleteSwfLastAprvReq(@RequestBody SwfLastAprvReqDTO swfLastAprvReqDTO) {
        swfLastAprvReqService.deleteSwfLastAprvReq(swfLastAprvReqDTO);
        return new Result<>();
    }



    @PostMapping("addDaPaiAprvReq")
    @ApiOperation("添加大牌最终审设置")
    public Result addDaPaiAprvReq(@RequestBody SwfLastAprvAddDaPaiDTO swfLastAprvAddDaPaiDTO) {
        Result result = new Result<>();
        int size = swfLastAprvAddDaPaiDTO.getDaPaiAddDataDTOList().size();
        List<SwfDocPhaseDefEntity> swfDocPhaseDefEntities
                = swfDocPhaseDefService.checkDocPhaseDef(swfLastAprvAddDaPaiDTO.getCompId(),swfLastAprvAddDaPaiDTO.getDocTypeValue());
        if (swfDocPhaseDefEntities.size() > 0) {
            return result.error(ErrorCode.DOC_TYPE_ERROR);
        } else {
            List<Long> approveId = new ArrayList<>();
            for (SwfLastAprvDaPaiAddDataDTO swfLastAprvDaPaiAddDataDTO : swfLastAprvAddDaPaiDTO.getDaPaiAddDataDTOList()) {
                approveId.add(swfLastAprvDaPaiAddDataDTO.getApproval());
            }
            HashSet set = new HashSet(approveId);
            approveId.clear();
            approveId.addAll(set);
            int setSize = approveId.size();
            if (setSize < size) {
                return result.error(ErrorCode.PHASE_DOC_REPEAT);
            }
            swfLastAprvReqService.addDaPaiAprvReq(swfLastAprvAddDaPaiDTO);
        }
        return result;
    }


    @PostMapping("editDaPaiAprvReq")
    @ApiOperation("修改大牌最终审设置")
    public Result editDaPaiAprvReq(@RequestBody SwfLastAprvAddDaPaiDTO swfLastAprvAddDaPaiDTO) {
        Result result = new Result<>();
        int size = swfLastAprvAddDaPaiDTO.getDaPaiAddDataDTOList().size();
        List<Long> approveId = new ArrayList<>();
        for (SwfLastAprvDaPaiAddDataDTO swfLastAprvDaPaiAddDataDTO : swfLastAprvAddDaPaiDTO.getDaPaiAddDataDTOList()) {
            approveId.add(swfLastAprvDaPaiAddDataDTO.getApproval());
        }
        HashSet set = new HashSet(approveId);
        approveId.clear();
        approveId.addAll(set);
        int setSize = approveId.size();
        if (setSize < size) {
            return result.error(ErrorCode.PHASE_DOC_REPEAT);
        }
        swfLastAprvReqService.editDaPaiAprvReq(swfLastAprvAddDaPaiDTO);
        return result;
    }


    @PostMapping("deleteDaPaiAprvReq")
    @ApiOperation("删除大牌最终审设置")
    public Result deleteDaPaiAprvReq(@RequestBody SwfLastAprvAddDaPaiDTO swfLastAprvAddDaPaiDTO) {
        swfLastAprvReqService.deleteDaPaiAprvReq(swfLastAprvAddDaPaiDTO);
        return new Result<>();
    }


    @PostMapping("getDaPaiAprvReqInfo")
    @ApiOperation("获取当前最终审设置")
    @LogOperation("获取当前最终审设置")
    public Result<SwfLastAprvAddDaPaiDTO> getDaPaiAprvReqInfo(@RequestBody SwfLastAprvAddDaPaiDTO swfLastAprvAddDaPaiDTO) {
        SwfLastAprvAddDaPaiDTO swfLastAprvAddDaPaiDTO1 =
                swfLastAprvReqService.getDaPaiAprvReqInfo(swfLastAprvAddDaPaiDTO.getCompId(), swfLastAprvAddDaPaiDTO.getDocTypeValue());
        return new Result<SwfLastAprvAddDaPaiDTO>().ok(swfLastAprvAddDaPaiDTO1);
    }
}
